package com.example.healkep.component.page;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.example.healkep.R;
import com.example.healkep.adapter.diseaseItemAdapter;

import java.util.ArrayList;
import java.util.List;

public class PageView extends LinearLayout implements OnRefreshListener, OnLoadMoreListener {
    private SwipeToLoadLayout swipeToLoadLayout;
    private TextView tv_pageIndex;
    private RecyclerView recyclerView;
    private int pageIndex = 1;
    private boolean isLastPage = false;
    private boolean isShowIndex = true;
    private OnLastPageListener lastPageListener;
    private OnNextPageListener nextPageListener;
    public PageView(Context context) {
        this(context,null);
    }
    public PageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public PageView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }
    public PageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }


    private void initView(Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.pageview_layout, this, true);
        tv_pageIndex = view.findViewById(R.id.tv_pageIndex);
        recyclerView = view.findViewById(R.id.swipe_target);
        swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL));
    }
    private void updateIndex(){
        String str;
        if(isLastPage){
            str = "最后一页";
        }else{
            str = "第"+pageIndex+"页";
        }
        if(isShowIndex){
            tv_pageIndex.setText(str);
        }else{
            tv_pageIndex.setVisibility(GONE);
        }
    }
    public void setShowIndex(boolean isShowIndex){
        this.isShowIndex = isShowIndex;
    }
    public void setLastPage(boolean isLastPage){
        this.isLastPage = isLastPage;
    }
    public boolean isShowIndex(){
        return this.isShowIndex;
    }
    public boolean isLastPage(){
        return this.isLastPage;
    }
    public RecyclerView getRecycleView(){
        return recyclerView;
    }

    public void setLastPageListener(OnLastPageListener lastPageListener){
        this.lastPageListener = lastPageListener;
    }
    public void setNextPageListener(OnNextPageListener nextPageListener){
        this.nextPageListener = nextPageListener;
    }
    public void setAdapter(RecyclerView.Adapter<?> adapter){
        if(adapter!=null)
            recyclerView.setAdapter(adapter);
    }
    public void setPageIndex(int index){
        this.pageIndex = index;
        updateIndex();
    }
    public int getPageIndex(){
        return pageIndex;
    }
    @Override
    public void onLoadMore() {
        if(!isLastPage){
            swipeToLoadLayout.setLoadingMore(true);
            nextPageListener.nextPage(this.pageIndex,recyclerView.getAdapter());
        }else{
            swipeToLoadLayout.setLoadingMore(false);
        }
    }

    @Override
    public void onRefresh() {
        if(pageIndex != 1){
            swipeToLoadLayout.setRefreshing(true);
            lastPageListener.lastPage(this.pageIndex,recyclerView.getAdapter());
        }else{
            swipeToLoadLayout.setRefreshing(false);
        }
    }
    public void pageFinished(){
        if(swipeToLoadLayout.isRefreshing()){
            //向前翻页结束
            swipeToLoadLayout.setRefreshing(false);
            pageIndex--;
            recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount()-1);
        }
        if(swipeToLoadLayout.isLoadingMore()){
            //向后翻页结束
            swipeToLoadLayout.setLoadingMore(false);
            pageIndex++;
            recyclerView.scrollToPosition(0);
        }

        updateIndex();
    }
    public interface OnLastPageListener{
        void lastPage(int nowPageIndex, RecyclerView.Adapter<?> adapter);
    }
    public interface OnNextPageListener{
        void nextPage(int nextPageIndex,RecyclerView.Adapter<?> adapter);
    }
}
